<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
if($_RQ['op']=='get'){
	$status=isset($_RQ['status'])?$_RQ['status']:1;
	$return['params']=array('status'=>$status);
	$return['ids']='';
	$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->store_id));
	$return['store']=$store;
	$return['isopenvoice']=$store['isopenvoice'];
	set_html('title','订单列表');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$this->store_id,'status'=>1,'deleted'=>0,'userdeleted'=>0);
    $orderby='id desc';
	$total='';
	$_RQ['ordersn']=$_RQ['ordersn']?:$_RQ['keyword'];
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['status']) && $_RQ['status']!='all'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	$return['list']=$this->getslice('order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
		$merchs =array();
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			$goodsinfo['image']=tomedia($goodsinfo['image']);
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
		}
		$item['pingjiatime'] = pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid'=>0),'createtime');
		if($item['pingjiatime']){
			$endpingjiatime = $item['pingjiatime'] + 7*24*3600;
			if($endpingjiatime>TIMESTAMP){
				$item['pingjia']=1;//1修改评价；2追加评价
			}else{
				$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid >'=>0),'id');
				if($zuipingjia<1){
					$item['pingjia']=2;
				}
			}
		}
		$item['merchs']=$merchs;
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		if($item['send_status']){
			$item['statustitle']=$this->send_status[$item['send_status']];
		}else{
			$item['send_status']=0;
			$item['statustitle']=$this->order_status[$item['status']];
		}
		$item['buytypetitle']=$this->buy_types[$item['buytype']];
		$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		$item['can_refund']=$this->model('order/refund')->check_order_rerefund($item['id']);
		$item['member']=cfc('core')->member_show($item['mid']);
		$address=$item['address']?json_decode($item['address'],true):$item['addressid'];
		if($address){
			$item['addressinfo']=$address;
			$item['address']=model('core/member')->get_address_str($address);
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$params['storeid']=$this->store_id;
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('1'=>'微信支付','2'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'金额',
		'paytype'=>'订单状态',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='fa'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['express_type']==0){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
				$this->model('order')->order_send($id,$data);
			}elseif($post['express_type']==1){//平台配送
				if(!$post['peisong_id'] && $post['need_setpeisong']){
					exi('请选择配送员！','error');
				}
				if(!$post['peisong_id'] && $post['set_peisonguser']==1){
					$post['peisong_id']=model('jy_peisong/order')->order_pai($post);
					$post['order_pai']=1;
				}
				if($post['peisong_id']){
					$post['order_id']=$id;
					if(!$post['order_pai']){
						model('jy_peisong/order')->order_sendget($post);
					}
					$this->model('express')->updata($post);
				}else{
					$data['send_status']=12;
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
					$this->model('order')->order_send($id,$data);
				}			
				exi('订单配送处理成功！','','/store.manage.order/');
			}elseif($post['express_type']==2){//商家自己配送
				$this->model('order')->order_send($id);
				exi('订单配送处理成功！','','/store.manage.order/');
			}elseif($post['express_type']==3){//达达配送
				$res=$this->model('order/dada_api')->dada_send($post);
				if($res['state']==1){
					$data=array('opensend_type'=>1);
					$this->model('order')->order_send($id,$data);
				}else{
					exi($res['msg'],'error');
				}
			}elseif($post['express_type']==4){//货拉拉配送
				$res=model('jy_huolala/huo_api')->order_send($post);
				if($res['state']==1){
					$post['opensend_type']=2;
					$post['openorderid']=$res['openorderid'];
					$this->model('order')->order_send($id,$post,$isup);
				}else{
					exi($res['msg'],'error');
				}
			}
			
		}elseif($post['sendtype']==3){
			$this->model('order')->order_send_virtual($id,$post['content']);
		}elseif($post['sendtype']==2){
			$this->model('verify')->self_verify($post['verifycode']);
		}		
		exi('已发货！','','/store.manage.order/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','is_peisong','sendtype','storeid','merchid','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	$item['sendtitle']='运费'.$item['dispatchprice'].'元';
	if($item['dispatchid']){
		$sendtypetitle=pdo_getcolumn('jy_weishop_dispatch',array('id'=>$item['dispatchid'],'uniacid'=>$_SESSION['uniacid']),'title');
	}else{
		$sendtypetitle='快递';
	}
	if($item['sendtype']==1 || $item['sendtype']==4){
		$citypeisong=$this->plugin_setting('citypeisong');
		$item['set_peisonguser']=$citypeisong['set_peisonguser'];
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
			if(!$item['isdish'] && $item['is_peisong']==2){
				$item['express_type']=0;
			}
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}elseif(!$item['express_type']){
				$tip='商品订单已打包好,快递发货！';
				$item['sendtitle'] .='('.$sendtypetitle.')';
			}
			
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
		}
		
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0)),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('express_type'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！'),
		);
		$return['express']=$this->model('model')->express_types();
	}else{
		$goodsid=$this->pdo_getcolumn('order_goods',array('orderid'=>$id),'goodsid');
		$goods=$this->pdo_get('goods',array('id'=>$goodsid),array('title','image'));
		$item['verifycode']=pdo_getcolumn('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),'number');
		$item=array_merge($goods,$item);
		$htmls['edit']=array(
			array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'image','read'=>true),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
			array('type'=>'text','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
			
		);
	}
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
			$users=pdo_getall('jy_peisong_member',array('group_id'=>$v['id'],'status'=>1,'online >'=>0),array('mid','title'));
			foreach($users as $uv){
					$peisongs[]=$uv;
			}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	set_html('title','订单发货');
	exi($return);
}
if($_RQ['op']=='postfa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if($id){
			if($post['sendtype']==1){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
				model('jy_weishop/order')->order_send($id,$data);
			}else{
				model('jy_weishop/order')->order_send_virtual($id,$post['content']);
			}			
			exi('发货成功！','','jy_weishop/store.manage.order/');
		}elseif($_SESSION['os']=='windows'){
			exi('参数错误','error');
		}	
	}
}
if($_RQ['op']=='peisong'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['peisong_id']){
			exi('请选择配送员！','error');
		}
		$params['order_id']=$id;
		$params['peisong_id']=$post['peisong_id'];
		model('jy_peisong/order')->order_get($params);		
		exi('指定配送员成功！');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype','storeid'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单指定配送',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','help'=>'只能指定该门店下面的配送员'),
	);
	$peisongs=array();
	$areas=pdo_getall('jy_peisong_area',array('store_id'=>$item['storeid']));
	foreach($areas as $item){
		$users=pdo_getall('jy_peisong_member',array('group_id'=>$item['id'],'status'=>1),array('mid','title'));
		foreach($users as $uv){
			$peisongs[]=$uv;
		}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	$this->model('order')->order_receive($id);
	exi('确认收货成功！');
}
if($_RQ['op']=='ispay'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('ispay'=>1,'status'=>1),array('id'=>$id));
	
	exi('已确认付款','');
	
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$print_type=18;
	$res=$this->model('printmsg')->storeprint('',$order_id,'','','','','','',$print_type,array('is18'=>1));
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}
if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>1);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'order.detail','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_delete($id);
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->model('order')->order_delete($id);
			}
		}
	}
	exi('删除成功！','','list');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！','','deleted');
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	$post=get_postdata();	
	if($post['price']){
		if(!is_numeric($post['price'])){
			exi('价格必须是数字！','error');
		}
		if(!method_exists(cfc('pay'),'change_pay_fee')){
			exi('修改价格失败！请联系管理员，更新框架到最新','error');
		}
		$price=floatval($post['price']);
		if ($price < 0) 
		{
			$price = 0;
		}
		$this->model('order')->upprice($post['id'],$price);
		exi('改价成功！','','jy_weishop/store.manage.order/');	
	}
	$item=$this->get('order',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='cancelfh'){
	$post=get_postdata();
	$id=intval($post['id'])?:$_RQ['id'];
	$this->model('order')->order_cancelfh($id);
	exi('取消发货成功！','','/store.manage.order/');	
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'uniacid'=>$_SESSION['uniacid'],
		'storeid'=>$this->store_id,
	);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
?>